import 'dart:io';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bmfmap_example/http/http.dart';
import 'package:flutter_bmfmap_example/models/userinformodel.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:image_picker/image_picker.dart';
import 'package:provider/provider.dart';

import 'modifyintro.dart';
import 'modifynamepage.dart';

class InformationPage extends StatefulWidget {
  InformationPage({Key key}) : super(key: key);

  @override
  _InformationPageState createState() => _InformationPageState();
}

class _InformationPageState extends State<InformationPage>
    with SingleTickerProviderStateMixin {
  var _imgPath;
  File _avataFile;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // backgroundColor: Color.fromRGBO(252, 252, 189, 1),

      appBar: AppBar(
        centerTitle: true,
        title: Text(
          '个人信息',
          style: TextStyle(
            fontSize: 16,
            color: Color.fromRGBO(72, 72, 72, 1),
            fontWeight: FontWeight.bold,
          ),
        ),
        backgroundColor: Colors.white,
        // backgroundColor: Color.fromRGBO(252, 252, 189, 1),
        elevation: 0,
        iconTheme: IconThemeData(
          color: Colors.black54
        ),
      ),
      body: Container(
        child: Stack(
          children: [
            Positioned(
              bottom: 0,
              child: Container(
                child: Image.asset(
                  'assets/images/common/minepage/dg.png',
                  width: 80,
                ),
              ),
            ),
            Positioned(
              bottom: 0,
              right: 0,
              child: Container(
                child: Image.asset(
                  'assets/images/common/minepage/bg.png',
                  width: 88,
                ),
              ),
            ),
            ListView(
              physics: const AlwaysScrollableScrollPhysics(
                  parent: BouncingScrollPhysics(),),
              children: [
                InkWell(
                  onTap: () {
                    _pickImage();
                  },
                  child: Container(
                      height: 80,
                      decoration: BoxDecoration(
                          color: Colors.white,
                          border: Border(
                              // 四个值 top right bottom left
                              bottom: BorderSide(
                                  // 设置单侧边框的样式
                                  color: Color(0xffEDEDED),
                                  width: 1,
                                  style: BorderStyle.solid))),
                      child: Padding(
                        padding: const EdgeInsets.only(left: 10, right: 10),
                        child: Row(
                          children: [
                            Text(
                              '头像',
                              style: TextStyle(
                                fontWeight: FontWeight.bold,
                                color: Color.fromRGBO(72, 72, 72, 1),
                              ),
                            ),
                            Spacer(),
                            Container(
                              width: 65,
                              height: 65,
                              child: _ImageView(),
                            ),
                            Icon(
                              Icons.keyboard_arrow_right,
                              color: Colors.grey,
                            ),
                          ],
                        ),
                      )),
                ),
                InkWell(
                  onTap: () {
                    Navigator.push(
                        context,
                        MaterialPageRoute(
                            builder: (context) => ModifynamePage()));
                  },
                  child: Container(
                      height: 60,
                      decoration: BoxDecoration(
                          color: Colors.white,
                          border: Border(
                              // 四个值 top right bottom left
                              bottom: BorderSide(
                                  // 设置单侧边框的样式
                                  color: Color(0xffEDEDED),
                                  width: 1,
                                  style: BorderStyle.solid))),
                      child: Padding(
                        padding: const EdgeInsets.only(left: 10, right: 10),
                        child: Row(
                          children: [
                            Text(
                              '昵称',
                              style: TextStyle(
                                fontWeight: FontWeight.bold,
                                color: Color.fromRGBO(72, 72, 72, 1),
                              ),
                            ),
                            Spacer(),
                            Text(
                              context.watch<Userinfomodel>().getnickname(),
                              style: TextStyle(
                                color: Colors.grey,
                              ),
                            ),
                            Icon(
                              Icons.keyboard_arrow_right,
                              color: Colors.grey,
                            ),
                          ],
                        ),
                      )),
                ),
                InkWell(
                  onTap: () {
                    Navigator.push(
                        context,
                        MaterialPageRoute(
                            builder: (context) => ModifyintroPage()));
                  },
                  child: Container(
                      height: 60,
                      decoration: BoxDecoration(
                          color: Colors.white,
                          border: Border(
                              // 四个值 top right bottom

                              bottom: BorderSide(
                                  // 设置单侧边框的样式
                                  color: Color(0xffEDEDED),
                                  width: 1,
                                  style: BorderStyle.solid))),
                      child: Padding(
                        padding: const EdgeInsets.only(left: 10, right: 10),
                        child: Row(
                          children: [
                            Text(
                              '简介',
                              style: TextStyle(
                                fontWeight: FontWeight.bold,
                                color: Color.fromRGBO(72, 72, 72, 1),
                              ),
                            ),
                            Spacer(),
                            Text(
                              context.watch<Userinfomodel>().getauthorization(),
                              style: TextStyle(
                                color: Colors.grey,
                              ),
                            ),
                            Icon(
                              Icons.keyboard_arrow_right,
                              color: Colors.grey,
                            ),
                          ],
                        ),
                      )),
                ),
                Container(
                    height: 60,
                    decoration: BoxDecoration(
                        color: Colors.white,
                        border: Border(
                            // 四个值 top right bottom left
                            bottom: BorderSide(
                                // 设置单侧边框的样式
                                color: Color(0xffEDEDED),
                                width: 1,
                                style: BorderStyle.solid))),
                    child: Padding(
                      padding: const EdgeInsets.only(left: 10, right: 10),
                      child: Row(
                        children: [
                          Text(
                            '用户名',
                            style: TextStyle(
                              fontWeight: FontWeight.bold,
                              color: Color.fromRGBO(72, 72, 72, 1),
                            ),
                          ),
                          Spacer(),
                          Text(
                            context.read<Userinfomodel>().getuserId(),
                            style: TextStyle(
                              color: Colors.grey,
                            ),
                          ),
                          Icon(
                            Icons.keyboard_arrow_right,
                            color: Colors.grey,
                          ),
                        ],
                      ),
                    )),
                InkWell(
                  onTap: () {
                    // Navigator.push(context,
                    //     MaterialPageRoute(builder: (context) => caddre()));
                  },
                  child: Container(
                    height: 60,
                    decoration: BoxDecoration(
                        color: Colors.white,
                        border: Border(
                            // 四个值 top right bottom left
                            bottom: BorderSide(
                                // 设置单侧边框的样式
                                color: Color(0xffEDEDED),
                                width: 1,
                                style: BorderStyle.solid))),
                    child: Padding(
                      padding: const EdgeInsets.only(left: 10, right: 10),
                      child: Row(
                        children: [
                          Text(
                            '地址',
                            style: TextStyle(
                              fontWeight: FontWeight.bold,
                              color: Color.fromRGBO(72, 72, 72, 1),
                            ),
                          ),
                          Spacer(),
                          Text(
                            '昆明理工大学',
                            style: TextStyle(
                              color: Colors.grey,
                            ),
                          ),
                          Icon(
                            Icons.keyboard_arrow_right,
                            color: Colors.grey,
                          ),
                        ],
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  _pickImage() async {
    File image = await ImagePicker.pickImage(source: ImageSource.gallery);

    _upLoadImage(image);
    setState(() {
      _avataFile = image;
    });
    setState(() {});
  }

  _upLoadImage(File image) async {
    String path = image.path;
    var name = path.substring(path.lastIndexOf("/") + 1, path.length);
    FormData formData = new FormData.fromMap(
        {"imageFile": await MultipartFile.fromFile(image.path)});

    var respone = await Http.post("/upload/image", data: formData);
    print(respone);
    Fluttertoast.showToast(
        msg: "图片上传成功", gravity: ToastGravity.CENTER, textColor: Colors.grey);
    // UserHepler.getInstance.setportraiturl(respone.data);

    _updateportrait(respone);
    context.read<Userinfomodel>().setportraiturl(respone);
  }

  _updateportrait(String url) async {
    print('********************************');
    print(url);
    print('********************************');
    Map<String, String> params = new Map();
    params["url"] = url;
    var respone = await Http.post("/user/upload", params: params)
        .catchError((value) => Fluttertoast.showToast(msg: "网络错误"));
    print(respone);
    Fluttertoast.showToast(
        msg: "头像更新成功", gravity: ToastGravity.CENTER, textColor: Colors.grey);
  }

  /*图片控件*/
  Widget _ImageView() {
    return Card(
      shape: RoundedRectangleBorder(
          borderRadius: BorderRadiusDirectional.circular(50)),
      clipBehavior: Clip.antiAlias,
      // child: Image.file(
      //   _avataFile,
      // ),

      child: Image.network(
        context.read<Userinfomodel>().getportraiturl(),
      ),
    );
  }
}
